package com.lp.multi.thread.example05;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lp
 * @create: 2021-06-22 14:57
 * @description: 不安全集合ArrayList
 */
public class SafeList {
    public static void main(String[] args) throws InterruptedException {
        List<String> arrayList = new ArrayList<String>();
        for(int i=0;i<10000;i++){
            new Thread(()->{
                //锁住变化的共享对象
                synchronized(arrayList){
                    arrayList.add(Thread.currentThread().getName());
                }
            }).start();
        }
        //不休眠，子线程可能还没有跑完
        Thread.sleep(3000);
        System.out.println("arrayList.size() = " + arrayList.size());
    }
    
    
}
